package me.fung.keen.app.nav

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import me.fung.keen.app.ShowAble
import me.fung.keen.app.Visual
import me.fung.keen.app.route.Route

abstract class BaseNavigation : Navigation, ShowAble {
    abstract val route: Route
    protected var views: MutableList<Visual> = ArrayList()
    private val viewer = Viewer()
    override fun goto(path: String, params: List<Any>) {
        val view = route.obtain(path, params)
        viewer.update { view }
        views.add(view)
    }

    override fun switchTo(path: String, params: List<Any>) {
        val view = route.obtain(path, params)
        viewer.update { view }
        views.removeLast()
        views.add(view)
    }

    override fun goBack() {
        views.removeLast()
        val view = views.last()
        viewer.update { view }
    }

    override fun goHome() {
        val view = views[0]
        views.clear()
        views.add(view)
        viewer.update { view }
    }

    override fun pageCount(): Int {
        val d = viewer.data ?: return 0
        return views.indexOf(d) + 1
    }

    @Composable
    override fun show(modifier: Modifier) {
        val view = remember { viewer }.data
        view?.show(modifier)
    }

    override fun goBack(pageCount: Int) {
        val view = views[pageCount]
        viewer.update { view }
    }
}


